package cn.edu.hqu.cst.android.ls.sensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class Sensor_Manager extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    EditText etOrientation;
    EditText etField;
    EditText etLight;
    EditText etPressure;
    EditText etTemprature;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor__manager);

        etOrientation = (EditText)findViewById(R.id.etOrientation);
        etField = (EditText)findViewById(R.id.etField);
        etLight = (EditText)findViewById(R.id.etLight);
        etPressure =(EditText)findViewById(R.id.etPressure);
        etTemprature=(EditText)findViewById(R.id.etTemperature);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

    }
    protected void onResume()	{
        super.onResume();
        // 为系统的方向传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);


        //为系统的磁场传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);

        //为系统的光传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);

        //为系统的压力传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);

        //为系统的温度传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);
    }
    @Override
    protected void onPause(){
        // 取消注册
        mSensorManager.unregisterListener(this);
        super.onPause();
    }
    @Override
    protected void onStop(){
        // 取消注册
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
         float[] values = event.values;
         //获取触发event的传感器类型
        int sensorType = event.sensor.getType();
        StringBuilder sb = null;
        //判断哪个传感器发生改变
        switch (sensorType){
            //方向传感器
            case Sensor.TYPE_ORIENTATION:
                sb=new StringBuilder();
                sb.append("绕Z轴转过的角度： ");
                sb.append(values[0]);
                sb.append("\n绕X轴转过的角度： ");
                sb.append(values[1]);
                sb.append("\n绕Y轴转过的角度： ");
                sb.append(values[2]);
                etOrientation.setText(sb.toString());
                break;
            //磁场传感器
            case Sensor.TYPE_MAGNETIC_FIELD:
                sb=new StringBuilder();
                sb.append("绕X轴转过的角度： ");
                sb.append(values[0]);
                sb.append("\n绕Y轴转过的角度： ");
                sb.append(values[1]);
                sb.append("\n绕Z轴转过的角度： ");
                sb.append(values[2]);
                etField.setText(sb.toString());
                break;
             //温度传感器
            case Sensor.TYPE_AMBIENT_TEMPERATURE:
                sb=new StringBuilder();
                sb.append("当前温度为： ");
                sb.append(values[0]);
                etTemprature.setText(sb.toString());
                break;
             // 光传感器
            case Sensor.TYPE_LIGHT:
                sb=new StringBuilder();
                sb.append("当前光强为： ");
                sb.append(values[0]);
                etLight.setText(sb.toString());
                break;
            // 压力传感器
            case Sensor.TYPE_PRESSURE:
                sb=new StringBuilder();
                sb.append("当前压力为： ");
                sb.append(values[0]);
                etPressure.setText(sb.toString());
                break;

        }



    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
